package com.withoutwords.ui.custom;

import java.util.ArrayList;

import com.withoutwords.R;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.Game;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;

public class GamesExpandableListAdapter extends BaseExpandableListAdapter {

	static final float childTextSize = 28;
	static final float groupTextSize = 40;
	ArrayList<Game> games = new ArrayList<Game>();
	
	Handler actionHandler = null;
	  private LayoutInflater mInflater;
	Context context;
	

	public GamesExpandableListAdapter(Context contxt,Handler handler) {
		this.context = contxt;
		 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 actionHandler = handler;
		
	}

	public void setGames(ArrayList<Game> newGameSet) {
		games = newGameSet;
	}
	public Game getGameAt(int index) {
		return games.get(index);
	}

@Override
public boolean isEmpty() {
	// TODO Auto-generated method stub
	return (games.size() == 0);
}

	public Object getChild(int groupPosition, int childPosition) {
		return games.get(groupPosition).getPlayer(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		if(games.size() > 0)
		{
			//return player count -1 , to exclude the owner
		return games.get(groupPosition).getPlayersCount()-1;
		}
		return 0;
	}

	public MyCustomTextView getGenericView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				AbsListView.LayoutParams.MATCH_PARENT, 64);

		MyCustomTextView textView = new MyCustomTextView(context);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
				// Set the text starting position
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		 
		MyCustomTextView textView = getGenericView();
		textView.setTextSize(Common.adjustTextSize(childTextSize, context));

		textView.setText(getChild(groupPosition, childPosition).toString());
		return textView;
	}

	public Object getGroup(int groupPosition) {
		return games.get(groupPosition);
	}


@Override
	public int getGroupCount() {
		return games.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		
		 if (convertView == null) {
			 convertView = mInflater.inflate(R.layout.game_list_group, parent, false);
         } 	
		 
		 MyCustomTextView textView =(MyCustomTextView)convertView.findViewById(R.id.txtGameOwnerName);
		 String ownername  = getGroup(groupPosition).toString();
		 if(ownername.equals(""))
		 {
			ownername = context.getResources().getString(R.string.friend);
		 }
		textView.setText(ownername);
		textView.setTextSize(Common.adjustTextSize(groupTextSize, context));

		MyCustomButton actionBtn  = (MyCustomButton)convertView.findViewById(R.id.btnPlay);
		
		actionBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				Bundle data = new Bundle();
				data.putSerializable("game", games.get(groupPosition));
				msg.setData(data);
				
				actionHandler.sendMessage(msg);
				
			}
		});
		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	public boolean hasStableIds() {
		return true;
	}

}
